package com.us.usws.site.console.action;

import javax.inject.Inject;

import net.iwsh.core.security.digest.DigestUtils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.us.usws.biz.LoginOutWorker;
import com.us.usws.common.SiteConstants;
import com.us.usws.model.domain.system.User;
import com.us.usws.model.flat.system.DOUser;
import com.us.usws.service.ro.UserROService;
import com.us.usws.support.uitls.SiteWebContext;
import com.us.usws.support.uitls.WebContext;

public class LoginAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private static Log log = LogFactory.getFactory().getInstance(LoginAction.class);

	/*
	 * bean properties
	 */
	private String loginName;
	private String passwd;
	private String act;

	@Inject
	private UserROService service;

	/**
	 * main handling method of struts2
	 */
	public String execute() {
		//directly goto input while 'act' param was empty.
		if (act == null || act.trim().length() == 0) {
			return INPUT;
		}

		//check user.
		User user = service.getUserByLogin(loginName, passwd);
		if (user == null) {
			//login failure
			log.warn("Login failed. Maybe you can try [admin/abc123] which md5 code is [" + DigestUtils.md5Salt("abc123") + "].");
			addFieldError("passwd", getText("login.fail"));
			return INPUT;
		}

		return "success";
	}

	/**
	 * admin login
	 */
	public void ajaxLogin() {
		WebContext wc = new SiteWebContext(getRequest(), getResponse());
		DOUser user = wc.getUser();

		if (user == null) {
			if (SiteConstants.IS_DEBUG) {
				user = service.getUserByLoginName(SiteConstants.AUTO_LOGIN_NAME);

			} else {
				user = service.getUserByLogin(loginName, passwd);
			}
		}

		//determine log user
		if (user != null) {
			log.debug("Login success.");
			LoginOutWorker.adminLogin(user, wc);
			getOut().print(true);
		} else {
			log.debug("Login failed.");
			getOut().print(false);
		}

	}

	/**
	 * ajax check user is or not login
	 */
	public void ajaxCheckIsLogin() {
		if (getSession().getAttribute(SiteConstants.SESSION_KEY_LOGIN_USER) == null) {
			getOut().print(false);
		} else {
			getOut().print(true);
		}
	}

	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.ActionSupport#validate()
	 */
	@Override
	public void validate() {

		if (act == null || act.trim().length() == 0) {
			//the 'act' param was not specified, which mean the form's at first show
			//now should not display error messages.
			return;
		}

		if (loginName == null || loginName.trim().length() == 0) {
			addFieldError("loginName", getText("error.empty.loginName"));
		}
		if (passwd == null || passwd.trim().length() == 0) {
			addFieldError("passwd", getText("error.empty.passwd"));
		}
	}

	/**
	 * @return the loginName
	 */
	public String getLoginName() {
		return loginName;
	}

	/**
	 * @param loginName the loginName to set
	 */
	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	/**
	 * @return the passwd
	 */
	public String getPasswd() {
		return passwd;
	}

	/**
	 * @param passwd the passwd to set
	 */
	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}

	/**
	 * @return the act
	 */
	public String getAct() {
		return act;
	}

	/**
	 * @param act the act to set
	 */
	public void setAct(String act) {
		this.act = act;
	}

}
